﻿using RBAC_Application.Repository.Interface;
using RBAC_Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC_Application.Repository.Implement
{
    public class PermissionRepository : IPermissionRepository
    {
        private readonly ISqlSugarClient _db;

        public PermissionRepository(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 根据权限ID获取权限详情
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns>权限对象</returns>
        public Permission GetById(long id)
        {
            return _db.Queryable<Permission>().First(p => p.Id == id);
        }

        /// <summary>
        /// 获取所有权限列表
        /// </summary>
        /// <returns>权限列表</returns>
        public List<Permission> GetAll()
        {
            return _db.Queryable<Permission>().ToList();
        }

        /// <summary>
        /// 根据关键字和模块获取权限列表
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="module">模块名称</param>
        /// <returns>权限列表</returns>
        public List<Permission> GetByCondition(string keyword, string module)
        {
            var query = _db.Queryable<Permission>();
            
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(p => p.PermissionName.Contains(keyword) || p.Description.Contains(keyword));
            }
            
            if (!string.IsNullOrEmpty(module))
            {
                query = query.Where(p => p.Module == module);
            }
            
            return query.ToList();
        }

        /// <summary>
        /// 根据角色ID获取权限列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>权限列表</returns>
        public List<Permission> GetPermissionsByRoleId(long roleId)
        {
            // 这里需要实现角色权限关联查询
            // 假设有一个RolePermission关联表
            return _db.Queryable<Permission>()
                .InnerJoin<RolePermission>((p, rp) => p.Id == rp.PermissionId)
                .Where((p, rp) => rp.RoleId == roleId)
                .Select((p, rp) => p)
                .ToList();
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="permission">权限对象</param>
        public void Add(Permission permission)
        {
            _db.Insertable(permission).ExecuteCommand();
        }

        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="permission">权限对象</param>
        public void Update(Permission permission)
        {
            _db.Updateable(permission).ExecuteCommand();
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id">权限ID</param>
        public void Delete(long id)
        {
            _db.Ado.BeginTran();
            try
            {
                // 先删除角色权限关联
                _db.Deleteable<RolePermission>().Where(rp => rp.PermissionId == id).ExecuteCommand();
                // 再删除权限
                _db.Deleteable<Permission>().Where(p => p.Id == id).ExecuteCommand();
                _db.Ado.CommitTran();
            }
            catch
            {
                _db.Ado.RollbackTran();
                throw;
            }
        }

        /// <summary>
        /// 判断权限名称是否已存在
        /// </summary>
        /// <param name="permissionName">权限名称</param>
        /// <returns>true表示已存在，false表示不存在</returns>
        public bool Exists(string permissionName)
        {
            return _db.Queryable<Permission>().Any(p => p.PermissionName == permissionName);
        }

        /// <summary>
        /// 分配角色权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionId">权限ID</param>
        public void AssignRolePermission(long roleId, long permissionId)
        {
            var rolePermission = new RolePermission
            {
                RoleId = roleId,
                PermissionId = permissionId
            };
            _db.Insertable(rolePermission).ExecuteCommand();
        }

        /// <summary>
        /// 移除角色权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="permissionId">权限ID</param>
        public void RemoveRolePermission(long roleId, long permissionId)
        {
            _db.Deleteable<RolePermission>()
                .Where(rp => rp.RoleId == roleId && rp.PermissionId == permissionId)
                .ExecuteCommand();
        }

        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>权限列表</returns>
        public List<Permission> GetRolePermissions(long roleId)
        {
            return _db.Queryable<Permission>()
                .InnerJoin<RolePermission>((p, rp) => p.Id == rp.PermissionId)
                .Where((p, rp) => rp.RoleId == roleId)
                .Select((p, rp) => p)
                .ToList();
        }
    }
}
